package com.ssyu.examination.exam.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.Constants;
import com.ssyu.examination.conmmon.util.FastjsonUtil;
import com.ssyu.examination.exam.entity.AnswerEntity;
import com.ssyu.examination.questionbank.entity.QesExamPaperEntity;
import com.ssyu.examination.questionbank.entity.QuestionQesEntity;
import com.ssyu.examination.questionbank.service.QesExamPaperService;

@WebServlet("/ExammingServlet")
public class ExammingServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public ExammingServlet() {
		super();
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("showPaper".equals(command)) {
			showPaper(request, response);
		} else if ("toShowPaperUI".equals(command)) {
			toShowPaperUI(request, response);
		} else if ("anser".equals(command)) {
			anser(request, response);
		} else if ("sub".equals(command)) {
			sub(request, response);
		}
	}

	/**
	 * 转到试卷页面
	 * 
	 * @throws IOException
	 * @throws ServletException
	 */
	private void toShowPaperUI(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		print(response, FastjsonUtil.toJson(id));
	}

	/**
	 * 进入考试试卷的展示
	 * 
	 * @param request
	 * @param response
	 */
	private void showPaper(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		QesExamPaperService examPaperService = new QesExamPaperService();

		QesExamPaperEntity qesExamPaperEntity = examPaperService.previewPaper(id);
		if (qesExamPaperEntity != null) {
			String URL = qesExamPaperEntity.getPaperUrl();
			try {
				File file = new File(URL);
				if (file.isFile() && file.exists()) { // 判断文件是否存在
					InputStreamReader read = new InputStreamReader(new FileInputStream(file), Constants.ENCODE_UTF8);// 考虑到编码格式
					BufferedReader bufferedReader = new BufferedReader(read);
					String lineTxt = null;
					StringBuilder str = new StringBuilder();
					while ((lineTxt = bufferedReader.readLine()) != null) {
						str.append(lineTxt);
					}
					List<Object> dataList = new ArrayList<>();
					dataList.add(str);
					dataList.add(new Date());
					print(response, FastjsonUtil.toJsonWithDateFormat(dataList, "yyyy-MM-dd HH:mm"));
					read.close();
				} else {
					print(response, false);
				}
			} catch (Exception e) {
				System.out.println("读取文件内容出错");
				e.printStackTrace();
			}
		}
	}

	/**
	 * 通过json得到一个答案
	 * 
	 * @param request
	 * @param response
	 */
	private void anser(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		System.out.println(id);
		QesExamPaperService examPaperService = new QesExamPaperService();

		QesExamPaperEntity qesExamPaperEntity = examPaperService.previewPaper(id);
		if (qesExamPaperEntity != null) {
			String str = examPaperService.accordJsonString(qesExamPaperEntity);
			if (str != null) {
				AnswerEntity answer = new AnswerEntity();
				String singleString = JSON.parseObject(str).getString("singleList");
				int singleOptionEachPoint = Integer
						.parseInt(JSON.parseObject(str.toString()).getString("singleOptionEachPoint"));
				List<QuestionQesEntity> singleList = JSON.parseArray(singleString, QuestionQesEntity.class);
				for (QuestionQesEntity questionQesEntity : singleList) {
					answer.setId(questionQesEntity.getId());
					answer.setAnswer(questionQesEntity.getAnswer());
					answer.setScore(singleOptionEachPoint);
				}
				String miltiString = JSON.parseObject(str).getString("miltiList");
				int multiOptionEachPoint = Integer
						.parseInt(JSON.parseObject(str.toString()).getString("multiOptionEachPoint"));
				List<QuestionQesEntity> miltiList = JSON.parseArray(miltiString, QuestionQesEntity.class);
				for (QuestionQesEntity questionQesEntity : miltiList) {
					answer.setId(questionQesEntity.getId());
					answer.setAnswer(questionQesEntity.getAnswer());
					answer.setScore(multiOptionEachPoint);
				}
				String judgeString = JSON.parseObject(str).getString("miltiList");
				int judgeEachPoint = Integer.parseInt(JSON.parseObject(str.toString()).getString("judgeEachPoint"));
				List<QuestionQesEntity> judgeList = JSON.parseArray(judgeString, QuestionQesEntity.class);
				for (QuestionQesEntity questionQesEntity : judgeList) {
					answer.setId(questionQesEntity.getId());
					answer.setAnswer(questionQesEntity.getAnswer());
					answer.setScore(judgeEachPoint);
				}
				List<AnswerEntity> answerList = new ArrayList<>();
				answerList.add(answer);
				Map<Integer, List<AnswerEntity>> answerMap = new HashMap<>();
				answerMap.put(id, answerList);
			}
		}

	}

	/**
	 * 提交试卷
	 * 
	 * @param request
	 * @param response
	 */
	private void sub(HttpServletRequest request, HttpServletResponse response) {
		String judgeName = request.getParameter("multipleName");
		System.out.println(judgeName);
		System.out.println("sfnsjd");
	}

}
